package com.wifi.eshipewifi;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wifi.BaseApplication;
import com.wifi.imp.MessageListener;

public class PackageCurrSettingActivity extends AppCompatActivity implements View.OnClickListener,MessageListener {
    ImageButton back;
    EditText et_wifi_ssid;
    LinearLayout ll_package_background;
    RelativeLayout rl_wifi_sure;
    TextView tittle;
    TextView tv_package;
    TextView tv_wifi_ssid;
    BaseApplication application;
    String TAG="WifiSettingActivity";
    Handler mhandler=new Handler(){//发送消息处理
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    };
    SharedPreferences sp;
    String sim;
    String curr_ll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_package_setting);
        application = (BaseApplication) getApplication();

        et_wifi_ssid = (EditText) findViewById(R.id.et_wifi_ssid);
        ll_package_background = (LinearLayout) findViewById(R.id.ll_package_background);
        rl_wifi_sure = (RelativeLayout) findViewById(R.id.rl_wifi_sure);
        back = (ImageButton) findViewById(R.id.ib_back);
        tittle = (TextView) findViewById(R.id.tv_include_tittle);
        tv_wifi_ssid = (TextView) findViewById(R.id.tv_wifi_ssid);
        tv_package = (TextView) findViewById(R.id.tv_package);
        tittle.setText("当前流量设置");
        tv_wifi_ssid.setText("设置已使用流量");
        back.setOnClickListener(this);
        rl_wifi_sure.setOnClickListener(this);
        ll_package_background.getBackground().setAlpha(100);
    }
    boolean curr_isGB;
    String package_unit;
    @Override
    protected void onResume() {
        super.onResume();
        application.setMsgListener(this);

        sp= getSharedPreferences("package", Activity.MODE_PRIVATE);
        String ssid = sp.getString("packageSetting", "4");//todo 以后要改
        sim = sp.getString("sim", "sim1");//todo 以后要改
        curr_ll = sp.getString("curr_ll", "0");//todo 以后要改
        curr_isGB = sp.getBoolean("curr_isGB", false);
        if(curr_isGB){
            tv_package.setText("GB");
            package_unit="GB";
        }else {
            tv_package.setText("MB");
            package_unit="MB";
        }
        et_wifi_ssid.setText(curr_ll);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_back:
                this.finish();
                break;

            case R.id.rl_wifi_sure:
                String ssid=et_wifi_ssid.getText().toString().trim();
                double curr_ll;
                if(curr_isGB){//GB
                    curr_ll=Math.ceil(Double.valueOf(ssid)*1024*1024);
                }else {//MB
                    curr_ll=Math.ceil(Double.valueOf(ssid)*1024);
                }
                if(ssid!=null ){
                    application.makeToast(PackageCurrSettingActivity.this,"上传成功 套餐:"+ssid+package_unit,true);
//                    $set_curr_data;sim1,1234KB;\r\n
                    application.sendMsg("$set_curr_data;"+sim+","+curr_ll+"KB"+";");
                }
                break;
        }
    }

    @Override
    public void MessageGetListener(String msg) {
        try {
            if (msg != null) {
                String s = msg;
                if (s.trim().length() > 0) {
                    Log.i(TAG, "mhandler接收到obj=" + s);
                    String[] s1=s.split(";");
                    if(s1[0].indexOf("set_curr_data")!=-1){
//                        //todo 断线重连 后台账号密码重连
                        String name = et_wifi_ssid.getText().toString().trim();
//                        //保存修改后的ssid
//                        SharedPreferences.Editor edit = sp.edit();
//                        edit.putString("packageSetting",name);
//                        //提交当前数据
//                        edit.commit();
                        //使用toast信息提示框提示成功写入数据
                        application.makeToast(PackageCurrSettingActivity.this,"修改成功",true);
                    }
                }
            }
            Log.i(TAG, "mhandler接收到msg" + msg);
        } catch (Exception ee) {
            Log.i(TAG, "加载过程出现异常");
            ee.printStackTrace();
        }
    }
}
